
const app = getApp()
Page({
  data: {
    index:0,
    add:true,
    addressItem:{},
  },
  addAddress(){
    let _this = this;
    app.util.request({
      url: "api/terminus/add",
      method: "post",
      data:{
        recipients:_this.data.recipients,
        phone:_this.data.phone,
        address:_this.data.address,
        acquiescent:_this.data.acquiescent,
      },
      success(response) {
          if (response.data.code == 0) {
            console.log(response)
              _this.setData({
                 address_list: response.data.data
              })
          } else {
            wx.showToast({
              title: response.data.msg,
              icon: 'none'
            })
          }
      }
  })
  },
  changeRecipients(e){
    this.setData({
      'addressItem.recipients':e.detail
    })
  },
  changePhone(e){
    this.setData({
      'addressItem.phone':e.detail
    })
  },
  changeAddress(e){
    this.setData({
      'addressItem.address':e.detail
    })
  },
  changeAcquiescent(e){
    this.setData({
      'addressItem.acquiescent':e.detail?1:0
    })
  },
  onLoad: function (options) {
    if(options.index!==undefined){
      this.setData({
        index:options.index,
        add:false
      })
      this.getAddressList()
    }else{
      this.setData({
        addressItem:{
          recipients:'',
          phone:'',
          address:'',
          acquiescent:0
        },
      })
    }
  },
getAddressList(){
  let _this = this;
  app.util.request({
      url: "api/terminus/findall",
      method: "get",
      success(response) {
      
          if (response.data.code == 0) {
            console.log(response)
              _this.setData({
                 addressItem:response.data.data[_this.data.index]
              })
          }
      }
  })
},
saveAddress(){
  let _this = this;
  let url = _this.data.add?'api/terminus/add':'api/terminus/update'
    app.util.request({
      url: url,
      method: "post",
      data:_this.data.addressItem,
      success(response) {
          if (response.data.code == 0) {
            wx.showToast({
              title: '保存地址成功',
            })
            
            wx.navigateBack({
              delta:2
            })
          } else {
            wx.showToast({
              title: response.data.msg,
              icon: 'none'
            })
          }
      }
  })
},
})
